package com.dbs.donatebooksystem.controller;

import com.dbs.donatebooksystem.Service.UserService;
import com.dbs.donatebooksystem.pojo.Result;
import com.dbs.donatebooksystem.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@CrossOrigin(origins = "http://localhost:8080",originPatterns = "http://localhost:8081")
public class LoginController {
     @Autowired
   private UserService userservice;
//登录接口
     @PostMapping("/Login")
    public Result UserLogin(@RequestBody User user){
         log.info("用户登录:{}",user);
         User u = userservice.login(user);//返回u，如果u为空则返回失败，不为空则成功
         if(u == null) {
             return Result.error("用户名不存在\n或不能为空");
         }
         log.info("用户状态为:{}",u.getStatus());
         if (Objects.equals(u.getStatus(),"0")) {
             log.warn("用户状态异常: {}", user.getAccount());
             return Result.error("用户已被禁用");
         }
         if(user.getPassword().equals(u.getPassword())) {
             return Result.success(u);
         }
         return Result.error("密码错误");

     }




}
